package com.gmss.crawler.module.retraction.company.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gmss.crawler.constants.Base;
import com.gmss.crawler.constants.Constants;
import com.gmss.crawler.enums.CommonEnum;
import com.gmss.crawler.module.retraction.company.domain.RCompany;
import com.gmss.crawler.module.retraction.company.service.IRCompanyService;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 撤稿论文-单位表 前端控制器
 * </p>
 *
 * @author Auto-generator
 * @since 2021-12-22
 */
@RestController
@RequestMapping("/rCompany")
public class RCompanyController extends Base {


    @Resource
    private IRCompanyService iRCompanyService;

    /**
     * 查询列表
     */
    @PostMapping("/list")
    public String list(@RequestBody RCompanyReqVo rCompany) {
        final LambdaQueryWrapper<RCompany> lambda = new QueryWrapper<RCompany>().lambda();
        this.buildCondition(lambda, rCompany);
        return respJson(CommonEnum.SUCCESS, iRCompanyService.list(lambda));
    }

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public String page(@RequestBody RCompanyReqVo pageParam) {

        final LambdaQueryWrapper<RCompany> lambda = new QueryWrapper<RCompany>().lambda();
        this.buildCondition(lambda, pageParam);
        final IPage<RCompany> page = iRCompanyService.page(new Page<>(Constants.PAGE_NO, Constants.PAGE_SIZE), lambda);
        return respJson(CommonEnum.SUCCESS, page);

    }


    /**
     * 构造查询条件
     *
     * @param lambda
     * @param param
     */
    private void buildCondition(LambdaQueryWrapper<RCompany> lambda, RCompanyReqVo param) {
        if (null!=param.getId()) {
            lambda.eq(RCompany::getId, param.getId());
        }
        if (!StringUtils.isEmpty(param.getCompanyName())) {
            lambda.eq(RCompany::getCompanyName, param.getCompanyName());
        }
        if (!CollectionUtils.isEmpty(param.getCreateTimeList())) {
            lambda.ge(RCompany::getCreateTime, param.getCreateTimeList().get(0));
            lambda.le(RCompany::getCreateTime, param.getCreateTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getCreateUser())) {
            lambda.eq(RCompany::getCreateUser, param.getCreateUser());
        }
        if (!CollectionUtils.isEmpty(param.getUpdateTimeList())) {
            lambda.ge(RCompany::getUpdateTime, param.getUpdateTimeList().get(0));
            lambda.le(RCompany::getUpdateTime, param.getUpdateTimeList().get(1));
        }
        if (!StringUtils.isEmpty(param.getUpdateUser())) {
            lambda.eq(RCompany::getUpdateUser, param.getUpdateUser());
        }
        lambda.orderBy(true, false, RCompany::getId);
    }


    /**
     * 请求model
     */
    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    private static class RCompanyReqVo extends RCompany {
        private List<String> createTimeList; // 创建时间起止
        private List<String> updateTimeList; // 修改时间起止
    }


}
